package com.wedimob.scrapper.parser;

import com.wedimob.scrapper.data.Content;

/**
 * 
 * @author melouadi
 * 
 * @param <T>
 */
public abstract class Parser<T extends Content> {

	/**
	 * nextParser.
	 * 
	 */
	private Parser<?> nextParser;

	/**
	 * set sucessor parser for the chain of responsability.
	 * 
	 * @param aParser
	 *            next parser
	 * @return next parser
	 */
	public final Parser<?> setSuccessor(final Parser<?> aParser) {
		nextParser = aParser;
		return aParser;
	}

	/**
	 * parse content to build pojo.
	 * 
	 * @param content
	 *            data
	 * @return parsed pojo
	 */
	public final Content parse(final Content content) {
		if (accept(content)) {
			return parseContent(content);
		}

		if (nextParser != null) {
			return nextParser.parse(content);
		} else {
			throw new RuntimeException("cannot handle " + content);
		}
	}

	/**
	 * parse content to build pojo.
	 * 
	 * @param content
	 *            data
	 * @return parsed pojo
	 */
	public abstract T parseContent(final Content content);

	/**
	 * return true if parser can handle this type.
	 * 
	 * @param content
	 *            of content
	 * @return status
	 */
	public abstract boolean accept(final Content content);
}
